//#include "CollectAllPCResultCmd.h"
#include "MyCustomThread.h"


bool isLocalComputer(const wstring& pcName)
{
	bool res = false;

	const long INFO_BUFFER_SIZE = 100;

	TCHAR infoBuf[INFO_BUFFER_SIZE];
	DWORD bufCharCount = INFO_BUFFER_SIZE;

	GetComputerName(infoBuf, &bufCharCount);

	wstringstream a;
	a << infoBuf;

	wstring var(a.str().c_str());

	if(wcscmp(pcName.c_str(), var.c_str()) == 0)
		res = true;
	
	return res;
}


int main()
{	
	wstring myuser(L"Administrator");
	wstring mypassword(L"admin");
	wstring mydomain(L"venus.local");

vector<ADCM_Computer>* connectedComputers= new vector<ADCM_Computer>;
	vector <ADCM_Computer> *computers = new vector<ADCM_Computer>;


	bridget_communicator::getComputers(wstring(L"server"), mydomain, myuser, mypassword, *connectedComputers);
	bridget_communicator::save(wstring(L"server"),mydomain,*connectedComputers);
	/*ADCM_Computer cmp(wstring(L"FELIPE-VM"), wstring(L"Windows Server"));*/
	bridget_communicator::initCom();

	//cout<<"size comp: "<<computers->size()<<endl;

	for (int i=0 ; i<connectedComputers->size();i++)
	{
		string command("ping ");
		string name(connectedComputers->at(i).getCn().begin(),connectedComputers->at(i).getCn().end());
		
		cout<<"Proving connection to: "<<name;

		command.append(name);
		command.append(" -n 1 >> pingResult.txt");
		int r=	system(command.c_str());

		if(r==0)
		{
			wcout<<" = Conected OK "<<endl;
			computers->push_back(connectedComputers->at(i));
		}
		else
		{
			wcout<<" = Unable to connect "<<endl;
		}

	}

	cout<<"===== Connected Computers ====="<<endl;
	for (vector<ADCM_Computer>::iterator iter1 = computers->begin(); iter1 != computers->end(); iter1++)
	{		
		wcout<<"computer Name: "<<(*iter1).getCn()<<endl;
	}

	MyCustomThread ** pcThread = new MyCustomThread*[computers->size()];
	
	vector<ADCM_Computer>::iterator iter;

	int index = 0;
	
	ResultFormatter resFormatter;

	wstring emptyString (L"");
 
	for (iter = computers->begin(); iter != computers->end(); iter++)
	{				
		// if((*iter).getCn().compare(L"VMMOBSPC") == 0){
		if (isLocalComputer((*iter).getCn()))
			pcThread[index] = new MyCustomThread(resFormatter, *iter, emptyString, emptyString, emptyString,true);
		else pcThread[index] = new MyCustomThread(resFormatter, *iter, myuser, mypassword, mydomain, false);

		pcThread[index++]->Start(NULL);

		// }
	}

	// MyCustomThread th(cmp, myuser, mypassword, mydomain, false);
	// th.Start(NULL);
	//
	//while(true) 
	//{ 
	//	 cout << "Al sleep" << endl;
	//	Sleep(50000);
	//	cout << "Al Exit" << endl;
	//	for (int t = 0; t < computers->size(); t ++)
	//	{
	//		//if (pcThread[t] != NULL) {
	//			cout << "INDEX : " << t << endl;
	//			pcThread[t]->setExit(true);
	//		//}
	//	}
	//	cout << "al break" << endl;
	//	break;
	//	cout << "despues del break" << endl;
	//} 
	//cout << "Pasondo al Stop" << endl;

	//for (int t = 0; t < computers->size(); t ++)
	//{
	//	//if (pcThread[t] != NULL)
	//		pcThread[t]->Stop(true);
	//}
	
	bool ban=true;
	while (ban)
	{	Sleep(3000);

		int cont=0;
		for (int t = 0; t < computers->size(); t ++)
		{
			if (pcThread[t]->getExit()== true) {
				pcThread[t]->Stop(true);
				cont++;
			}
		}
		if(cont==computers->size())
			break;
	
	}


	Sleep(5000);

	bridget_communicator::closeCom();

	cout<<"End Collection"<<endl;
	//CollectAllPCResultCmd cmd;
	//cmd.execute();

	//	system("PAUSE");
}